package top.drinyeit.weilong.presenter;

import android.support.v7.widget.LinearLayoutManager;

import com.google.gson.Gson;

import top.drinyeit.weilong.activity.AppraiseActivity;
import top.drinyeit.weilong.adapter.AppraiseAdapter;
import top.drinyeit.weilong.model.AppraiseCommodityModel;
import top.drinyeit.weilong.retrofit.HttpResponse;
import top.drinyeit.weilong.retrofit.RetrofitMethod;
import top.drinyeit.weilong.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.weilong.retrofit.subscriber.SubscriberListener;

import static top.drinyeit.weilong.app.Constant.COMMON_SUCCESS;

/**
 * Created by wu on 2018/1/20.
 */

public class AppraisePresenter {
    private AppraiseActivity activity;
    private AppraiseAdapter appraiseAdapter;

    public AppraisePresenter(AppraiseActivity activity) {
        this.activity = activity;
    }

    //获取未评价商品(跳转订单评价页面)
    public void findAppraise(String children_order_id) {
        activity.showLoading();
        CommonSubscriber<HttpResponse<AppraiseCommodityModel>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<AppraiseCommodityModel>>() {
            @Override
            public void onError(String e, int code) {
                activity.dismissLoading();
                activity.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<AppraiseCommodityModel> o) {
                activity.dismissLoading();
                if (o.getCode() == COMMON_SUCCESS) {
                    appraiseAdapter = new AppraiseAdapter(o.getData());
                    activity.recyclerView.setLayoutManager(new LinearLayoutManager(activity));
                    activity.recyclerView.setAdapter(appraiseAdapter);
                } else {
                    activity.showToastMsg(o.getMessage());
                }
            }
        });
        RetrofitMethod.getInstance().findAppraise(subscriber, children_order_id);
    }

    //提交订单评价
    public void orderAppraise() {
        String order_appraise = new Gson().toJson(appraiseAdapter.getData());
        for (int i = 0; i < appraiseAdapter.getData().getCommodity_appraise_list().size(); i++) {
            if (appraiseAdapter.getData().getCommodity_appraise_list().get(i).getDescription_score() == null) {
                activity.showToastMsg("请先评价商品");
                return;
            }
        }
        if (appraiseAdapter.getData().getStore_appraise().getService_score() == null || appraiseAdapter.getData().getStore_appraise().getLogistics_score() == null) {
            activity.showToastMsg("请评价店铺");
            return;
        }
        activity.showLoading();
        CommonSubscriber<HttpResponse<String>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<String>>() {
            @Override
            public void onError(String e, int code) {
                activity.dismissLoading();
                activity.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<String> o) {
                activity.dismissLoading();
                activity.showToastMsg(o.getMessage());
                if (o.getCode() == COMMON_SUCCESS) {
                    activity.finish();
                }
            }
        });
        RetrofitMethod.getInstance().orderAppraise(subscriber, order_appraise);
    }
}
